package method

import (
	"database/sql"
	// "fmt"
	"reflect"

	"gitlab.gybin.com/GYbin/structure"
)

func CreateAllTablese(db *sql.DB) {
	UserTableFunc(db, structure.USER_TABLE)     //创建用户表
	PhotoCache(db, structure.PHOTO_CACHE)       //创建照片缓存表
	PhotodDir(db, structure.PHOTO_DIR)          //创建目录结构表
	SourcePhotoList(db, structure.SOURCE_PHOTO) //创建源照片信息
	ConfigTable(db, structure.CONFIG_TABLE)     //配置表
}

//根据结构类型，以及结构后面的tag 创建数据库结构
func createTablesStruct(td interface{}, db *sql.DB, tablename string) {

	t := reflect.TypeOf(td)
	createTableCmd := "CREATE TABLE IF NOT EXISTS "
	tableFieldStr := ""
	for i := 0; i < t.NumField(); i++ {
		if i != 0 {
			tableFieldStr = tableFieldStr + ","
		}
		field := t.Field(i)
		typeTag := field.Tag.Get(structure.SqlType)
		tableTag := field.Tag.Get(structure.SqlField)
		tableFieldStr = tableFieldStr + tableTag + " " + typeTag
		// fmt.Printf("%d. %v(%v), tag:'%v'\n", i+1, field.Name, field.Type.Name(), typeTag)
	}
	createTableCmd = createTableCmd + tablename + "(" + tableFieldStr + ")"
	// fmt.Println("sqlCmd:", createTableCmd)
	db.Exec(createTableCmd)
}
